package com.example.demo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import com.sun.nio.zipfs.ZipPath;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.Document;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/image")
public class TestController {
    @RequestMapping("/test")
    public String test(){
        return "test";
    }
    @RequestMapping("/testxieziban")
    public String testxieziban(){
        return "testxieziban";
    }
    @RequestMapping("/test2")
    public String test2(){
        return "test2yasuo";
    }
    @RequestMapping("/test3")
    public String test3(){
        return "test3";
    }
    @RequestMapping("/test4")
    public String test4(){
        return "test4";
    }
    @RequestMapping("/test5")
    public String test5(){
        return "test5";
    }
    @RequestMapping("upload.action")
    @ResponseBody
    public String uploadImage(HttpServletRequest request) throws IOException {
        String imgData = request.getParameter("imgData");
        System.out.println(imgData);
        //字节处处base64解码
        byte[] byteImageData=null;
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            byteImageData = base64Decoder.decodeBuffer(imgData);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-dd");
        String fileUrl = "D:\\test.png";
        File file = new File(fileUrl);
        file.createNewFile();
        OutputStream out = new FileOutputStream(file);
        out.write(byteImageData);
        out.flush();
        out.close();
        return "";
    }


    public static void main(String[] args) {
        Path path = FileSystems.getDefault().getPath("logs", "access.log");
        FileUtil.del(path);
    }

    /**
     * 发送xml格式http请求
     * @return
     */
    public void sendXml(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("key","value");
        Map<String,Object> map = jsonObject.toBean(Map.class);

        String paramXml = XmlUtil.mapToXmlStr(map);
        //转换会多出一个<xml>需要去掉
        paramXml.replace("<xml>","").replace("<xml>","").replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>","");
        HttpResponse execute = HttpUtil.createRequest(Method.POST, "请求地址").body(paramXml).header("Cookie", "xxxxx").execute();

    }

}
